import { http } from './http'
export interface ChangePasswordByCodeParams {
  identifier: string;

  type: 'email' | 'phone';

  credential: string;

  newPassword: string;
}

export interface AccountBindingParams {
  identifier: string;

  credential: string;
}

/**
 * 重置手机验证码
 * @param phoneNumber
 */
export function resetPhoneCredential (phoneNumber: string) {
  return http().post(`/api/account/ignores/account/login/phone/${phoneNumber}/code`)
}

/**
 * 重置邮箱验证码
 * @param email
 */
export function resetEmailCredential (email: string) {
  return http().post(`/api/account/ignores/account/email/${email}/code`)
}

/**
 * 判断账号是否已注册
 * @param identifier
 * @param type
 */
export function isAccountRegistered (identifier: string, type: 'username' | 'phone' | 'email') {
  return http().get(`/api/account/ignores/account/check/${identifier}/${type}/registered`)
}

/**
 * 判断账号是否已绑定
 * @param identifier
 * @param type
 */
export function isAccountBound (identifier: string, type: 'username' | 'phone' | 'email') {
  return http().get(`/api/account/ignores/account/check/${identifier}/${type}/bound`)
}

/**
 * 绑定手机号
 * @param phoneNumber
 * @param credential
 */
export function bindPhone (phoneNumber: string, credential: string) {
  return http().post(`/api/account/ignores/account/login/phone/${phoneNumber}/${credential}/bind`)
}

/**
 * 绑定用户名
 * @param username
 * @param password
 */
export function bindUsername (username: string, password: string) {
  return http().post(`/api/account/ignores/account/login/username/${username}/${password}/bind`)
}

/**
 * 注册手机号
 * @param phoneNumber
 */
export function registerPhone (phoneNumber: string) {
  return http().post(`/api/account/ignores/registry/phone/${phoneNumber}`)
}

/**
 * 修改密码。
 * @param originPassword
 * @param newPassword
 */
export function changePassword (originPassword: string, newPassword: string) {
  return http().post(`/api/account/account/username/password/${originPassword}/${newPassword}`)
}

/**
 * 校验密码。
 * @param password
 */
export function checkPassword (password: string) {
  return http().post('/api/account/account/username/password/check', {}, { params: { password } })
}

/**
 * 修改真实名称。
 * @param originPassword
 * @param newPassword
 */
export function changeName (name: string) {
  return http().post(`/api/account/users/user/name/${name}`)
}

/**
 * 绑定用户名到当前账号。
 * @param params
 */
export function bindUsernameToCurrent (params: AccountBindingParams) {
  return http().post('/api/account/account/bind/username', params)
}

/**
 * 绑定手机号到当前账号。
 * @param params
 */
export function bindPhoneToCurrent (params: AccountBindingParams) {
  return http().post('/api/account/account/bind/phone', params)
}

/**
 * 绑定邮箱到当前账号。
 * @param params
 */
export function bindEmailToCurrent (params: AccountBindingParams) {
  return http().post('/api/account/account/bind/email', params)
}

/**
 * 绑定钉钉到当前账号。
 * @param params
 */
export function bindDingtalkToCurrent (code: string) {
  return http().post('/api/account/account/bind/dingtalk', {}, { params: { code } })
}

/**
 * 绑定企业微信到当前账号。
 * @param params
 */
export function bindWecomToCurrent (code: string) {
  return http().post('/api/account/account/bind/wecom', {}, { params: { code } })
}

/**
 *  解绑手机号。
 */
export function unbindPhone () {
  return http().post('/api/account/account/unbind/phone')
}

/**
 *  解绑用户名。
 */
export function unbindUsername () {
  return http().post('/api/account/account/unbind/username')
}

/**
 *  解绑邮箱。
 */
export function unbindEmail () {
  return http().post('/api/account/account/unbind/email')
}

/**
 *  解绑钉钉。
 */
export function unbindDingtalk () {
  return http().post('/api/account/account/unbind/dingtalk')
}

/**
 *  解绑企业微信。
 */
export function unbindWecom () {
  return http().post('/api/account/account/unbind/wecom')
}

/**
 * 换绑手机号。
 * @param params
 */
export function rebindPhone (params: AccountBindingParams) {
  return http().post('/api/account/account/rebind/phone', params)
}

/**
 * 换绑用户名。
 * @param params
 */
export function rebindUsername (params: AccountBindingParams) {
  return http().post('/api/account/account/rebind/username', params)
}

/**
 * 换绑邮箱。
 * @param params
 */
export function rebindEmail (params: AccountBindingParams) {
  return http().post('/api/account/account/rebind/email', params)
}

/**
 * 获取当前用户特定类型身份标识。
 * @param phoneNumber
 * @param credential
 */
export function getIdentity (type: 'phone' | 'username' | 'email' | 'ding_talk' | 'wecom') {
  return http().get(`/api/account/account/identity/${type}`)
}

/**
 * 注销用户。
 */
export function logoff () {
  return http().post('/api/account/users/user/logoff')
}

/**
 * 检查凭证是否正确。
 * @param identifier
 * @param type
 * @param credential
 */
export function checkCredentialCorrected (identifier: string, type: 'phone' | 'email', credential: string) {
  return http().get(`/api/account/ignores/account/check/${identifier}/${type}/${credential}/corrected`)
}

/**
 * 判断是否绑定指定登录方式，并已经绑定了密码登录方式。
 * @param identifier
 * @param type
 * @returns
 */
export function isBoundWithPassword (identifier: string, type: 'phone' | 'email') {
  return http().get(`/api/account/ignores/account/check/${identifier}/${type}/bound/password`)
}

/**
 * 通过验证码修改密码。
 * @param params
 * @returns
 */
export function changePasswordByCode (params: ChangePasswordByCodeParams) {
  return http().post('/api/account/ignores/account/username/password', params)
}
